"""
中间件：每次请求和响应都会调用

"""
from django.http import HttpResponse


# TODO  对这个中间件的代码非常不懂了（说明对cookie也不懂，不清楚怎么获取
# TODO 主要是这个中间件的代码一开，请求来的时候根本就没带着cookie了
def simple_middleware(get_response):

    def middleware(req):

        print("---我在请求前---")
        response = get_response(req)  # <HttpResponseNotFound status_code=404, "text/html">
        print("---我在请求后---")
        # cookies = req.COOKIES
        # user_name = cookies.get('user_name')
        # print("user_name", end=":")
        # print(user_name)

        user_name = req.COOKIES.get('user_name')
        if user_name is None:
            print("user_name is None")
            return HttpResponse('你没登陆哈')
        return response
    return middleware
